package com.orhon.smartcampus.modules.user.fetcher;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class UsersFetcher implements DataFetcher {

    @Autowired
    UsersDao usersDao;

    @Autowired
    TokenService tokenService;

    @Override
    @Transactional
    public Object get(DataFetchingEnvironment env) throws Exception {

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        
        String userType = JwtUtils.getUserType(token);

        List<GQLUser> retUsers = new ArrayList<>();
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);

        String hql = "from Users where deleted_at = null ";
        
        if (env.containsArgument("condition")){
            HashMap<String, Object> map = ObjectToMap.to(env.getArgument("condition"));
            if (map.get("idCard") != null){
                hql += " and idcard "+Util.decide(map.get("idCard"));
            }
            if (map.get("userName") != null){
                hql += " and username "+Util.decide(map.get("userName"));
            }
            if (map.get("userType") != null){
                hql += " and user_type "+Util.decide(map.get("userType"));
            }
            if (map.get("school") != null){
                hql += " and school_id "+Util.decide(map.get("school"));
            }

        }
                hql += "order by created_at desc";
        List<Users> users = usersDao.find(hql, null , page.getPage(),  page.getLimit());

        for (Users user : users) {
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("data/school")){
                Schools schools = user.getSchool();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            GQLUser gqlUser = UserMapper.MAPPER.mapUser(user);
            gqlUser.setSchool(gqlSchool);
            retUsers.add(gqlUser);
        }

        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
            total = usersDao.getTotalCount(hql);
        }
        return Result.ok().put("data", retUsers).put("total", total);
    }


}
